package com.smallpineapple.turing.controller.admin;

/**
 * @author Jack
 * @date 2019-07-30-14:40
 */

import com.smallpineapple.turing.dao.MemberDao;
import com.smallpineapple.turing.dao.TeacherDao;
import com.smallpineapple.turing.dto.MemberDTO;
import com.smallpineapple.turing.dto.PeopleDTO;
import com.smallpineapple.turing.util.JsonResultUtil;
import com.smallpineapple.turing.dto.TeacherDTO;
import com.smallpineapple.turing.entity.Member;
import com.smallpineapple.turing.entity.Teacher;
import com.smallpineapple.turing.enums.MyCustomizeErrorCode;
import com.smallpineapple.turing.exception.CustomizeException;
import com.smallpineapple.turing.service.admin.TokenService;
import com.smallpineapple.turing.util.MD5Util;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "管理员登录接口")
@RestController
@CrossOrigin(value = "*", maxAge = 3600, allowCredentials = "true")
public class AdminLoginController {

    @Autowired
    MemberDao memberDao;
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    TokenService tokenService;

    @ApiOperation(value = "团队成员登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名(学号)",required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码",required = true, paramType = "query", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 2001, message = "找不到对应成员, 请检查后重新尝试请求!"),
            @ApiResponse(code = 3005, message = "密码不正确!请重新尝试!")
    })
    @PostMapping("/teamMember/login")
    public JsonResultUtil teamMemberLogin(@RequestParam(name = "username") String username,
                                          @RequestParam(name = "password") String password){

        Member dbMember = memberDao.findByMemberStuIdEquals(username);
        if(dbMember == null) {
            throw new CustomizeException(MyCustomizeErrorCode.MEMBER_NOT_FOUND);
        }
        MemberDTO memberDTO = new MemberDTO();
        if(dbMember.getPassword().equals(MD5Util.getHexPassword(password))){
            BeanUtils.copyProperties(dbMember, memberDTO);
            memberDTO.setRoleName(dbMember.getRole().getRoleName());
        }else{
            throw new CustomizeException(MyCustomizeErrorCode.PASS_NOT_CORRECT);
        }
        String token = tokenService.getMemberToken(dbMember);
        return JsonResultUtil.success().addObject("member", memberDTO).addObject("token", token);

    }
    @ApiOperation(value = "教师登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "教师用户名(邮箱)",required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码",required = true, paramType = "query", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 2001, message = "找不到对应成员, 请检查后重新尝试请求!"),
            @ApiResponse(code = 3005, message = "密码不正确!请重新尝试!")
    })
    @PostMapping("/teamTeacher/login")
    public JsonResultUtil teamTeacherLogin(@RequestParam(name = "username")String username,
                                           @RequestParam(name = "password")String password){

        Teacher dbTeacher = teacherDao.findTeacherByTeacherEmailEquals(username);
        if(dbTeacher == null){
            throw new CustomizeException(MyCustomizeErrorCode.MEMBER_NOT_FOUND);
        }
        TeacherDTO teacherDTO = new TeacherDTO();
        if(dbTeacher.getTeacherPassword().equals(MD5Util.getHexPassword(password))){
            BeanUtils.copyProperties(dbTeacher, teacherDTO);
            teacherDTO.setRoleName(dbTeacher.getRole().getRoleName());
        }else{
            throw new CustomizeException(MyCustomizeErrorCode.PASS_NOT_CORRECT);
        }
        String token = tokenService.getTeacherToken(dbTeacher);
        return JsonResultUtil.success().addObject("member", teacherDTO).addObject("token", token);

    }

    @ApiOperation(value = "退出登录")
    @ApiResponses(
            @ApiResponse(code = 3006, message = "尚未登录")
    )
    @PostMapping("/logout")
    public JsonResultUtil logout(HttpServletRequest request){
        PeopleDTO member = (PeopleDTO) request.getSession().getAttribute("member");
        if(member == null){
            throw new CustomizeException(MyCustomizeErrorCode.NOT_LOGIN);
        }
        return JsonResultUtil.success();
    }

}
